/**
 * 回款配票
 * @author miaoyl
 * @date 2019/07/11
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'GWEPMLEGAL_NAME', width: 160, title: '归属法人', fixed: 'left',sort: true}
            , {field: 'GWEPPLANT_NAME', width: 80, title: 'ERP法人'}
            , {field: 'GWEPDOCNO', width: 100, title: '核销单号',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEPDOCDATE', width: 100, title: '单据日期',search: 'default', searchOpts: {type: 'date'}}
            , {field: 'GWEP008_SUM', width: 100, title: '原币金额'}
            , {field: 'GWEP009_SUM', width: 100, title: '本币金额'}
            , {field: 'GWEP003_NAME', width: 100, title: '客户',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEP004_NAME', width: 100, title: '业务人员',search: 'default', searchOpts: {icon: true}}
            , {field: 'GWEPACTI', width: 100, title: '状态',
                filterDatas: app.datas_('crm_gwei009_gweoacti'),
                search: 'default',
                searchOpts: {type: 'select', default: ''}}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwei022setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei022SettingAction,
        moduleName = '回款配票'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei022',
                _searchs: [{
                    id: 'gwei022', tableId: 'gwei022_table', datas:  cols[0]
                }],
                _loadTplsFull: [{
                        dom: 'gwei022_edit', tpl: 'gwei022_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }, {
                        dom: 'common_open_ywy', tpl: 'common/_open_ywy'
                    }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei022_table'),
                    url: g_ctxPath + 'crm/gwei022/getGweplist',
                    exportsUrl: g_ctxPath + 'crm/gwei022/getGweplist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwei022_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: ''
                });

                layui.laytpl(app.getByid(doc, 'script-gwei022setting-action').innerHTML).render({}, function (string) {
                    gwei022SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei022_table)', function (obj) {

                    layer.tips(gwei022SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.GWEPACTI != 'N'){
                                            app.showWarning("只有状态为N：未审核的单据才可以编辑！");
                                            return ;
                                        }
                                        self._openTplFull({
                                            tag: 'gwei022_edit',
                                            tagData:{
                                                moduleName:moduleName,
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                minmax:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWEPACTI != 'N'){
                                            app.showWarning("只有状态为N：未审核的单据才可以删除！");
                                            return ;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei022_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei022_edit',
                        tagData: {
                            moduleName:moduleName,
                            obj:obj,
                            is_check:true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax:true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei022_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei022_edit',
                                tagData: {
                                    is_add: true,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax:true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'confirm':
                            self.confirm();
                            break;
                        case 'confirmCancel':
                            self.confirmCancel();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei022_table', where: table_param});
        },

        delData: function (obj) {
            app.ajax({
                xurl: 'crm/gwei022/del',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwei022_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWEP_T');
                    if(v.GWEPACTI != 'N'){
                        app.showWarning("只有状态为N：未审核的单据才可以删除！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEPDOCNO = v.GWEPDOCNO;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei022/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        confirm:function () {
            var checkStatus = table.checkStatus('gwei022_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWEP_T');
                    if(v.GWEPACTI != 'N'){
                        app.showWarning("只有状态为N：未确认的单据才可以确认！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEPDOCNO = v.GWEPDOCNO;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei022/updateStatus',
                        xdata: {_xdata:_xdata,ACTI:'Y',ACTI_OLD: 'N'},
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否确认选择的数据？"},
                        okmsg: moduleName + '信息确认成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        confirmCancel:function () {
            var checkStatus = table.checkStatus('gwei022_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWEP_T');
                    if(v.GWEPACTI != 'Y'){
                        app.showWarning("只有状态为Y：审核的单据才可以取消确认！");
                        flag = false;
                        return ;
                    }
                    _temp.GWEPDOCNO = v.GWEPDOCNO;
                    _xdata.push(_temp);
                });
                if(flag){
                    app.ajax({
                        xurl: 'crm/gwei022/updateStatus',
                        xdata: {_xdata:_xdata,ACTI:'N',ACTI_OLD: 'Y'},
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消确认选择的数据？"},
                        okmsg: moduleName + '信息取消确认成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei022',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWEP003_NAME') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                            url: 'crm/gwei022/getGwakData'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEP003 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWEP004_NAME') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '编号', prop: 'ID' , width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEP004_NAME = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWEPDOCNO') {//核销单号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            // method: 'q_gwepdocno',
                                            columnList: [
                                                {label: '核销单号', prop: 'GWEPDOCNO' , width: '150'},
                                                {label: '单据日期', prop: 'GWEPDOCDATE', width: '150'},
                                                {label: '人员', prop: 'GWEP004_NAME', width: '150'},
                                                {label: '状态', prop: 'GWEPACTI_NAME', width: '150'}
                                            ],
                                            url:"crm/gwei022/getGwecdocno"
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEPDOCNO = data.GWEPDOCNO;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择核销单号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});